(library (graph-utils)
  (export routes->path)
  (import (except (rnrs base)
                  let-values
                  map)
          (only (guile)
                lambda* λ)
          ;; SRFIs
          ;; hash tables
          (srfi srfi-69))

  (define routes->path
    (lambda* (routes target #:key (equal-test eq?))
      "Constructs the shortest path from the start node to the
target node using a routes table. A routes table is assumed
to be a hash table, which stores the preceding node on a
path to any node."
      (let iter ([current-node° target] [path° (list target)])
        (let ([prior-node (hash-table-ref routes current-node°)])
          (cond
           [(equal-test current-node° prior-node) path°]
           [else
            (iter prior-node (cons prior-node path°))]))))))
